package noodlesscm;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.beans.*;
import java.util.*;



class AddProduct {

	JLabel l1,l2,l3,l4,l5;
	JTextField t1,t2,t3,t4;
	JTextArea t5;
	JButton jb1,jb2;
	JPanel jp1,jp2;
        FileOutputStream fos;
        PrintStream ps;
        File file=new File("GUIProduct1.xml");

        //guiprosave gps=new guiprosave();
	public void addProduct()
	{
	JFrame jf=new JFrame("Addition of a New Product");
	jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	jf.setBackground(Color.white);
	jf.setSize(400,300);
	jf.setLocation(250,200);
        jf.setVisible(true);
	jf.setLayout(new BorderLayout(30,30));
	JLabel jl=new JLabel("Enter the following details of the product");
	jp1=new JPanel();
	jp1.setLayout(new GridLayout(1,1));
	jp1.add(jl);
	jf.add(jp1,BorderLayout.NORTH);
	jp2=new JPanel();
	jp2.setLayout(new GridLayout(6,2,10,10));


	l1=new JLabel("Name of the Product:");
	jp2.add(l1);
	t1=new JTextField();
	jp2.add(t1);
	l2=new JLabel("Unique ID of the Product:");
	jp2.add(l2);
	t2=new JTextField();
	jp2.add(t2);
	l3=new JLabel("Quantity of the Product:");
	jp2.add(l3);
	t3=new JTextField();
	jp2.add(t3);
	l4=new JLabel("Description of the Product:");
	jp2.add(l4);
	t4=new JTextField();
	jp2.add(t4);
	l5=new JLabel("Name of Inventories:");
	jp2.add(l5);
	t5=new JTextArea();
	JScrollPane areaScrollPane = new JScrollPane(t5);
	jp2.add(areaScrollPane);
	jb1=new JButton("Add");
	jp2.add(jb1);
	jb2=new JButton("Clear");
	jp2.add(jb2);
	jf.add(jp2,BorderLayout.CENTER);

	jb1.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent e)
	{
                try {
                        savetoFile1();
                    }
                catch (FileNotFoundException ex)
                    {
                        Logger.getLogger(AddProduct.class.getName()).log(Level.SEVERE, null, ex);
                    }
                catch (IOException ex)
                    {
                        Logger.getLogger(AddProduct.class.getName()).log(Level.SEVERE, null, ex);
                    }

            clear();
	}


	});


	jb2.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                clear();
            }
        });

	}

 private void savetoFile1() throws FileNotFoundException, IOException
            {
                        Product p = new Product();
                        fos=new FileOutputStream("GUIProduct.txt",true);
			ps=new PrintStream(fos);
                        p.setName(t1.getText());
                        p.setProductid(Integer.parseInt(t2.getText()));
                        p.setQuantity(Integer.parseInt(t3.getText()));
                        p.setDescription(t4.getText());
                        p.setInvenlist(t5.getText());
			ps.println(p.getName()+","+p.getProductid()+","+p.getQuantity()+","+p.getDescription()+","+p.getInvenlist());
                        XMLEncoder e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(file)));
                        e.writeObject(p);
                        e.close();
                        fos.close();
            }


    public void clear()
    {
	t1.setText("");
	t2.setText("");
	t3.setText("");
	t4.setText("");
	t5.setText("");
	return;
	}



    public void savetoxml()
    {

	String s=new String();
	s=t5.getText();
	System.out.println(s);
	int begin=0;
	Vector vs=new Vector();
	//System.out.println("After parsing textarea string\n");
	for(int i=0;i<s.length();i++)
            {
                if(s.charAt(i)=='\n')
                    {
                        vs.add(s.substring(begin,i));
                        begin=i+1;
                    }
            }

	vs.add(s.substring(begin));
	System.out.println(vs);

        ProductList a = new ProductList();
        Vector v = a.getProducts();
        Product q = new Product();
	Product p = new Product();
	ProductList obj=new ProductList();
	if(new File("products1.xml").exists())
        {
                try
                    {
                        XMLDecoder istream = new XMLDecoder(new BufferedInputStream(new FileInputStream("products1.xml")));
                        obj =(ProductList) istream.readObject();
                        istream.close();
                    }

                catch(Exception ex)
                    {
                        System.out.println(ex);
                    }
	}

	for(int i=0;i<obj.products.size();i++)
	{
            p=(Product) obj.products.get(i);
            v.add(p);
	}


        q.setName(t1.getText());
	q.setProductid(Integer.parseInt(t2.getText()));
	q.setQuantity(Integer.parseInt(t3.getText()));
	q.setDescription(t4.getText());
	//q.setInvenlist(vs);
        v.add(q);

	try {
                XMLEncoder xe = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("products1.xml")));
                xe.writeObject(a);
                //xe.flush();
                xe.close();
               
            }
        catch(Exception ex)
            {
                System.out.println(ex);
            }
    }

}
